package com.facetest.engine;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.facetest.utils.json.JsonMananger;
import com.tenginekit.model.FaceLandmarkInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 检查是否执行了该命令
 */
public class CheckFaceEngine {
    private static CheckFaceEngine checkFaceEngine;
    public static CheckFaceEngine getInstance(){
        if(checkFaceEngine==null){
            synchronized (CheckFaceEngine.class){
                checkFaceEngine = new CheckFaceEngine();
            }
        }
        return checkFaceEngine;
    }

    //眨眼
    public static final int ACTION_TYPE_EYE =0;
    //张嘴
    public static final int ACTION_TYPE_MOUTH = 1;
    //左右摇头
    public static final int ACTION_TYPE_LEFT_RIGHT_HEAD=2;
    //抬头
    public static final int ACTION_TYPE_RISE_HEAD=3;
    //低头
    public static final int ACTION_TYPE_BOW_HEAD=4;
    //没有正对屏幕
    public static final int ACTION_TYPE_NOT_ZHENGDUI=5;
    /**
     * 检测动作
     *
     */
    private int checkTime = 5000;//检测时间
    private long startTime = 0 ;
    private List<Integer> eyesActions = new ArrayList<>();
    private List<Integer> mouthActions = new ArrayList<>();
    private List<Integer> headActions = new ArrayList<>();
    private int zy1,zy2,zz1,zz2,zyyt1,zyyt2;
    public void onCheckAction(List<FaceLandmarkInfo> faces){
        Log.e("onCheckAction",faces.size()+"");
        if(faces==null||faces.size()==0){
            return;
        }
        if(startTime==0){
            startTime = System.currentTimeMillis();
            eyesActions = new ArrayList<>();
            mouthActions = new ArrayList<>();
            headActions = new ArrayList<>();
        }
        FaceLandmarkInfo landmarkInfo = faces.get(0);//取第一张脸的数据


        if(landmarkInfo.roll>10||landmarkInfo.roll<-10){
            //没有正对屏幕
            Message message = new Message();
            message.what = ACTION_TYPE_NOT_ZHENGDUI;
            handler.sendMessage(message);
        }else{
            if(landmarkInfo.yaw>3){
                headActions.add(1);
                zyyt1++;
            }else if(landmarkInfo.yaw<-3){
                headActions.add(0);
                zyyt2++;
            }

            if(landmarkInfo.pitch<-18){
                //抬头
                Message message = new Message();
                message.what = ACTION_TYPE_RISE_HEAD ;
                handler.sendMessage(message);
            }else if(landmarkInfo.pitch>18){
                //低头
                Message message = new Message();
                message.what = ACTION_TYPE_BOW_HEAD;
                handler.sendMessage(message);
            }else {

                if(landmarkInfo.leftEyeClose>0.85f||landmarkInfo.rightEyeClose>0.85){
                    eyesActions.add(1);
                    zy1++;
                }else{
                    eyesActions.add(0);
                    zy2++;
                }
                if(landmarkInfo.mouseOpenBig>0.85){
                    mouthActions.add(1);
                    zz1++;
                }else{
                    mouthActions.add(0);
                    zz2++;
                }
            }
        }


        if(zy1>30||zy2>30||zz1>30||zz2>30||zyyt1>30||zyyt2>30){
            restart();
            Message message = new Message();
            message.what = -1 ;
            handler.sendMessage(message);
            restart();
            return;
        }
        if(eyesActions.size()>4){
            Log.e("eyesActions", JsonMananger.beanToJson(eyesActions));
            if(checkHas(eyesActions)){
                Message message = new Message();
                message.what = ACTION_TYPE_EYE ;
                handler.sendMessage(message);
                restart();
            }
        }
        if(mouthActions.size()>4){
            if(checkHas(mouthActions)){
                Message message = new Message();
                message.what = ACTION_TYPE_MOUTH ;
                handler.sendMessage(message);
                restart();
            }
            Log.e("mouthActions", JsonMananger.beanToJson(mouthActions));
        }

        if(headActions.size()>4){
            if(checkHas(headActions)){
                Message message = new Message();
                message.what = ACTION_TYPE_LEFT_RIGHT_HEAD ;
                handler.sendMessage(message);
                restart();
            }
            Log.e("headActions", JsonMananger.beanToJson(mouthActions));
        }

        //pitch 人脸pitch方向转角
        //yaw 人脸yaw方向转角
        //roll 人脸roll反向转角



        //超时
        if(startTime!=0&&(System.currentTimeMillis()-startTime==checkTime)) {
            restart();
        }
    }
    //动作系数
    private float coefficient = 0.33f;
    private boolean checkHas(List<Integer> list){
        int count  = 0 ;
        for(Integer i:list){
            if(i==1){
                count++;
            }
        }
        if(count>0){
            return true;
        }
        return false;
    }
    //重新开始
    private void restart(){
        zy1 = 0;
        zy2= 0 ;
        zz1 = 0;
        zz2 = 0 ;
        zyyt1=0;
        zyyt2=0;
        eyesActions = new ArrayList<>();
        mouthActions = new ArrayList<>();
        headActions = new ArrayList<>();
        startTime = 0;
    }
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int type = msg.what;
            if(onFaceCheckResult!=null){
                onFaceCheckResult.onAction(type);
            }
        }
    };


    private OnFaceCheckResult onFaceCheckResult;

    public OnFaceCheckResult getOnFaceCheckResult() {
        return onFaceCheckResult;
    }

    public void setOnFaceCheckResult(OnFaceCheckResult onFaceCheckResult) {
        this.onFaceCheckResult = onFaceCheckResult;
    }

    //回调 检查是否有执行该动作
    public interface OnFaceCheckResult{
        //是否执行 执行类型
        void onAction(int type);
    }
}
